package com.blockmeta.bbs.minemodule.fragment;


import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blockmeta.bbs.baselibrary.net.callback.MyStringCallback;
import com.blockmeta.bbs.baselibrary.utils.SharedPreferenceUtil;
import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.baselibrary.utils.gson.GsonUtil;
import com.blockmeta.bbs.businesslibrary.base.fragment.BaseFragment;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.constant.NetConstant;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.minemodule.R;
import com.blockmeta.bbs.minemodule.R2;
import com.blockmeta.bbs.minemodule.adapter.FeedbackHistoryAdapter;
import com.blockmeta.bbs.minemodule.pojo.BBSLatestPOJO;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

/**
 * 反馈历史
 */
public class FeedBackHistoryFragment extends BaseFragment {

    @BindView(R2.id.rcv_feedback_list)
    RecyclerView mRcvFeedbackList;

    private List<BBSLatestPOJO.ListBean> data = new ArrayList<>();
    private FeedbackHistoryAdapter mAdapter;

    @Override
    public View setContentView(LayoutInflater inflater, ViewGroup container) {
        return LayoutInflater.from(mContext).inflate(R.layout.fragment_feed_back_history, null);
    }

    @Override
    public void initView() {
        mRcvFeedbackList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mAdapter = new FeedbackHistoryAdapter(R.layout.item_feedback_history, data, mContext);
        View view = LayoutInflater.from(mContext).inflate(R.layout.empty_data, null);
        mAdapter.setEmptyView(view);
        mRcvFeedbackList.setAdapter(mAdapter);
        network();
    }


    /**
     * 请求数据
     */
    private void network() {
        data.clear();
        OkHttpUtils.get()
                .addHeader("from", "android")
                .addHeader("token", SharedPreferenceUtil.getString(mContext, Constant.TOKEN, ""))
                .addHeader("uid", AccountHelper.getUid())
                .url(NetConstant.FEEDBACK_HISTORY)
                .build()
                .execute(new MyStringCallback() {


                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastUtil.show("联网失败，请检查网络");
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        BBSLatestPOJO mBbsLatestPOJO = null;
                        try {
                            mBbsLatestPOJO = GsonUtil.buildGson().fromJson(response, BBSLatestPOJO.class);
                        } catch (Exception e) {}
                        if (mBbsLatestPOJO == null) return;
                        if (mBbsLatestPOJO.getList() == null || mBbsLatestPOJO.getList().size() == 0)
                            return;
                        data.addAll(mBbsLatestPOJO.getList());
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }
}
